//
//  SmallDecisionsNswcResultViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/20.
//

#import "SmallDecisionsNswcResultViewController.h"

@interface SmallDecisionsNswcResultViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong) UIView *nswcContainerView;
@property(nonatomic,strong) UILabel *nswcTitleLabel;
@property(nonatomic,strong) UIButton *nswcSureBtn;
@property(nonatomic,strong) UIButton *nswcNewBtn;
@end

@implementation SmallDecisionsNswcResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self nswcContainerView];
    [self nswcTitleLabel];
    [self tableView];
    [self nswcNewBtn];
    [self nswcSureBtn];
}

#pragma markUITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.correntList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId" forIndexPath:indexPath];
    NSString *message = self.correntList[indexPath.row];
    cell.backgroundColor = SmallDecisionsBgColor;
    cell.textLabel.text = message;
    cell.textLabel.textAlignment = 1;
    cell.textLabel.textColor = SmallDecisionsThemeFontColor;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (UIView *)nswcContainerView {
    if(!_nswcContainerView) {
        _nswcContainerView = [[UIView alloc] init];
        _nswcContainerView.backgroundColor = SmallDecisionsPurpleColor;
        _nswcContainerView.layer.cornerRadius = 10;
        _nswcContainerView.layer.masksToBounds = YES;
        [self.view addSubview:_nswcContainerView];
        [_nswcContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
            make.centerY.mas_equalTo(-30);
        }];
    }
    return _nswcContainerView;
}

- (UILabel *)nswcTitleLabel {
    if(!_nswcTitleLabel) {
        _nswcTitleLabel = [[UILabel alloc] init];
        _nswcTitleLabel.textColor = SmallDecisionsThemeColor;
        _nswcTitleLabel.font = SmallDecisionsSetFontSize(18);
        _nswcTitleLabel.text = [NSString stringWithFormat:@"共答对%lu题目",(unsigned long)self.correntList.count];
        _nswcTitleLabel.textAlignment = 1;
        [self.nswcContainerView addSubview:_nswcTitleLabel];
        [_nswcTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _nswcTitleLabel;
}

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 40;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.separatorColor = SmallDecisionsSecondBgColor;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellId"];
        [self.nswcContainerView addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nswcTitleLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(MIN(kScreenHeight * 0.4, self.correntList.count * 40));

        }];
    }
    return _tableView;
}


- (UIButton *)nswcNewBtn {
    if(!_nswcNewBtn) {
        _nswcNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nswcNewBtn setTitle:@"再来一局" forState:UIControlStateNormal];
        [_nswcNewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_nswcNewBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _nswcNewBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(16);
        _nswcNewBtn.layer.cornerRadius = 30;
        _nswcNewBtn.layer.masksToBounds = YES;
        _nswcNewBtn.backgroundColor = SmallDecisionsBgColor;
        [_nswcNewBtn addTarget:self action:@selector(nswcNewBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.nswcContainerView addSubview:_nswcNewBtn];
        [_nswcNewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
            make.left.equalTo(self.nswcContainerView).offset(20);
            make.right.equalTo(self.nswcContainerView).offset(-20);
            make.top.equalTo(self.tableView.mas_bottom).offset(60);
        }];
    }
    return _nswcNewBtn;
}

- (UIButton *)nswcSureBtn {
    if(!_nswcSureBtn) {
        _nswcSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nswcSureBtn setTitle:@"回到主页" forState:UIControlStateNormal];
        [_nswcSureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_nswcSureBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _nswcSureBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(16);
        _nswcSureBtn.layer.cornerRadius = 30;
        _nswcSureBtn.layer.masksToBounds = YES;
        _nswcSureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_nswcSureBtn addTarget:self action:@selector(nswcSureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.nswcContainerView addSubview:_nswcSureBtn];
        [_nswcSureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
            make.left.equalTo(self.nswcContainerView).offset(20);
            make.right.equalTo(self.nswcContainerView).offset(-20);
            make.top.equalTo(self.nswcNewBtn.mas_bottom).offset(15);
            make.bottom.mas_equalTo(-30);
        }];
    }
    return _nswcSureBtn;
}

- (void)nswcNewBtnAction {
    if(self.callback) {
        self.callback(@{@"new":@(YES)});
    }
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:NO completion:nil];
}

- (void)nswcSureBtnAction {
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:NO completion:^{
        [SmallDecisionsNavigatorManager popToRootViewControllerAnimated:NO];
    }];
}


@end
